package cn.com.dhcc.turbo.web.controller.registry;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu.LEAF_MENU;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.PriorityConf;
import cn.com.dhcc.turbo.service.registry.DataCategoryService;
import cn.com.dhcc.turbo.service.registry.DomainInfoService;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
import cn.com.dhcc.turbo.service.registry.OrgInfoService;
import cn.com.dhcc.turbo.service.registry.PriorityConfService;

/**
 * 优先级配置Controller
 * 
 * @日期：2014-9-12上午11:11:11
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Controller
@RequestMapping("/registry")
@TurboMenu(belongToMenu = LEAF_MENU.PRIORITY_CONF)
public class PriorityConfController extends BaseController {
	
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	
	@Autowired
	private PriorityConfService priorityConfService;
	/*@Autowired
	private SysInfoService sysService;*/
	@Autowired
	private NodeInfoService nodeService;
	@Autowired
	private DomainInfoService domainService;
	@Autowired
	private OrgInfoService orgInfoService;
	@Autowired
	private DataCategoryService dcService;

	/**
	 * 跳转优先级配置页
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/priority_conf_mgr", method = RequestMethod.GET)
	public String priorityConfMgr(Model model) {
		return "registry/priority_conf_mgr";
	}
	/**
	 * 初始化数据树
	 * @param model
	 * @param response
	 * @param type　1：按发送数据分类　2：按接收系统　3：按接收节点　4：按发送系统　5：按业务域
	 */
	@RequestMapping(value = "/init_data_tree")
	public String initDataTree(Model model, HttpServletResponse response,String type) {
		try {
			model.addAttribute("treeData",priorityConfService.initDataTree(type));
			model.addAttribute("type",type);
		} catch (Exception e) {
			logger.error("获取数据树失败", e);
		}
		return "registry/priority_conf_data_tree";
	}
	@RequestMapping(value = "/data_tree")
	public void nodeTree(Model model, HttpServletResponse response,String type, String id,boolean select) {
		try {
			writeString(response, priorityConfService.dataTree(type,id,select));
		} catch (Exception e) {
			logger.error("获取节点树失败", e);
		}
	}
	
	@RequestMapping(value = "/own_info_get", method = RequestMethod.GET)
	public String get( Model model,String ownId,String type) {
		String page="";
		try {
			if("1".equals(type)){//1：按发送数据分类
				/*DataCategory vo = dcService.getById(ownId);
				model.addAttribute(DATA_KEY, vo);
				page = "registry/priority_dc_limit_conf";*/
			}else if("2".equals(type) || "4".equals(type)){//2：按接收系统  4：按发送系统
				//SysInfo vo = sysService.getInfo(ownId);
				//model.addAttribute(DATA_KEY, vo);
				page = "registry/priority_sys_limit_conf";
				
			}else if("3".equals(type)){//3：按接收节点
				
				NodeInfo vo = nodeService.get(ownId);
				model.addAttribute(DATA_KEY, vo);
				//model.addAttribute("sysInfoList",sysService.getSysInfoByNodeId(ownId));
				model.addAttribute("orgInfo",orgInfoService.get(vo.getNodeCode().substring(0, 6)));
				page = "registry/priority_node_limit_conf";
				
			}else if("5".equals(type)){//5：按业务域
				
				DomainInfo vo = domainService.get(ownId);
				model.addAttribute(DATA_KEY, vo);
				page = "registry/priority_domain_limit_conf";
				
			}
			PriorityConf vo = priorityConfService.getByOwnId(ownId);
			model.addAttribute("conf", vo);
			model.addAttribute("type", type);
		} catch (Exception e) {
			logger.error("获取节点信息失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return page;
	}
	/**
	 * 
	 * @param model
	 * @param vo
	 * @return
	 */
	@RequestMapping(value = "/add_priority_conf")
	@ResponseBody
	public Map<String, Object> add(Model model, PriorityConf vo) {
		try {
			String msg = "";
			vo.setMtime(new Date());
			vo.setStatus("1");
			if("".equals(vo.getId()) || vo.getId() == null){
				vo.setId(UUIDGenerator.getUUID());
				priorityConfService.add(vo);
				msg="添加成功";
			}else{
				priorityConfService.update(vo);
				msg="修改成功";
			}
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			logger.error("添加优先级配置信息失败", e);
			return jsonFailResult("添加优先级配置信息失败");
		}
	}
}
